package com.p2p.auth.service.context;

import com.p2p.auth.common.enums.ArticleOnTopType;
import com.p2p.auth.common.enums.ArticleStatus;
import com.p2p.auth.common.enums.ArticleType;
import com.p2p.auth.domain.entity.Article;
import com.p2p.auth.domain.dto.article.ArticleQueryDTO;
import com.p2p.auth.service.specification.ArticleSpecification;
import com.p2p.auth.service.DateFormatService;
import com.p2p.auth.service.repository.ArticleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 *
 * @author Antinomy
 * @date 17/12/4
 */
@Service
public class ArticleContext {

    @Autowired
    private ArticleRepository articleRepo;

    public void storeNewActicle(Article article) {
        String uuid = UUID.randomUUID().toString();
        article.setUuid(uuid);

        this.updatePubishDate(article);

        articleRepo.save(article);
    }

    public void store(Article article) {
        articleRepo.save(article);
    }


    public Page<Article> getArticlesByPage(ArticleQueryDTO dto) {
        return articleRepo.findAll(ArticleSpecification.buildFromDTO(dto),dto.getPage());
    }

    public List<HashMap> renderDtos(List<Article> articles) {
        List list = new ArrayList<>();

        for (Article article : articles) {
            HashMap item = new HashMap();

            item.put("id", article.getId());

            item.put("uuid", article.getUuid());
            item.put("title", article.getTitle());

            item.put("type", article.getType().getValue());

            item.put("dateCreated", DateFormatService.toTime(article.getDateCreated()));

            Date eventDate = article.getEventDate();
            if (eventDate != null) {
                item.put("eventDate", DateFormatService.toDay(eventDate));
            } else {
                item.put("eventDate", null);
            }

            item.put("clickCounts", article.getClickCounts().toString());
            item.put("onTop", article.getOnTop().getValue());

            item.put("status", article.getStatus().getValue());

            item.put("author", article.getAuthor());
            list.add(item);
        }

        return list;
    }

    public List renderActicles(Page<Article> articles) {
        List list = new ArrayList<>();

        for (Article article : articles) {
            list.add(renderActicle(article));
        }

        return list;
    }

    public HashMap renderActicle(Article article) {
        HashMap result = new HashMap();

        result.put("id", article.getUuid());
        result.put("title", article.getTitle());
        result.put("type", article.getType());
        result.put("channel", article.getChannel());

        result.put("ctsStr", DateFormatService.toDay(article.getEventDate()));
        result.put("ctsLongStr", DateFormatService.toTime(article.getEventDate()));
        result.put("picAddress", article.getImageUrl());
        result.put("videoUrl", article.getVideoUrl());

        result.put("summary", article.getSummary());
        result.put("contentStr", article.getContent());
        result.put("onTop", article.getOnTop());
        result.put("status", article.getStatus());


        result.put("author", article.getAuthor());

        return result;
    }

    public void updatePubishDate(Article article) {
        if(article.getStatus().equals(ArticleStatus.PUBLISHED)) {
            article.setEventDate(new Date());
        } else {
            article.setEventDate(null);
        }

    }

    public Article getArticleByUuid(String uuid) {
        return articleRepo.findByUuid(uuid);
    }

    public Article getArticleById(Long artiId) {
        return articleRepo.getOne(artiId);
    }

    public List<Article> getArticlesByType(ArticleType articleTypetype) {
        return articleRepo.findByTypeAndStatusNotLike(articleTypetype, ArticleStatus.DELETED);
    }

    public Article changeOnTopByUuid(String uuid) {
        Article article = articleRepo.findByUuid(uuid);
        ArticleOnTopType changedOnTop = changeOnTop(article);
        article.setOnTop(changedOnTop);

        article = articleRepo.saveAndFlush(article);
        return article;
    }

    public Article deleteByUuid(String uuid) {
        Article article = articleRepo.findByUuid(uuid);
        article.setStatus(ArticleStatus.DELETED);

        article = articleRepo.saveAndFlush(article);
        return article;
    }

    private ArticleOnTopType changeOnTop(Article article) {
        if (article.getOnTop().equals(ArticleOnTopType.NONE)){
            return ArticleOnTopType.ON_TOP;
        }

        return ArticleOnTopType.NONE;
    }

}
